/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.usuallyinterface;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.CommonMethodsUtil;
import com.lightkits.common.db.entity.FndOrganizationBase;
import com.lightkits.common.db.entity.FndOrganizationTl;
import com.lightkits.common.model.CurrentWorkerInfo;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.database.dao.WmsDeliveryTicketMapper;
import com.lightkits.wms.database.model.WmSimTicketModel;
import com.lightkits.wms.dto.usuallyinterface.GetSimTicketRequestType;
import com.lightkits.wms.dto.usuallyinterface.GetSimTicketResponseType;
import com.lightkits.wms.dto.usuallyinterface.model.SimpleTicketDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * GetSimTicketExecutor
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/4/19 15:37
 */
@Component
public class GetSimTicketExecutor implements ServiceExecutor<GetSimTicketRequestType, GetSimTicketResponseType> {
    private final WmsDeliveryTicketMapper wmDeliveryTicketMapper;

    @Autowired
    public GetSimTicketExecutor(WmsDeliveryTicketMapper wmDeliveryTicketMapper) {
        this.wmDeliveryTicketMapper = wmDeliveryTicketMapper;
    }

    @Override
    public GetSimTicketResponseType service(GetSimTicketRequestType request) {
        GetSimTicketResponseType response = new GetSimTicketResponseType();
        response.setSuccessful(true);
        //获取当前员工信息
        CurrentWorkerInfo currentWorkerInfo =
                CommonMethodsUtil.getCurrentWorkerInfo(null);
        FndOrganizationBase base = new FndOrganizationBase().selectOne(
                new EntityWrapper()
                        .eq(FndOrganizationTl.ORGANIZATION_ID,
                                currentWorkerInfo.getDefaultOrganizationId()));
        if (ObjectUtil.isEmpty(base)) {
            response.setSuccessful(false);
            response.setMessage("get orgCode failed");
            return response;
        }
        //获取送货单头列表信息
        List<WmSimTicketModel> models =
                wmDeliveryTicketMapper.querySimTicketByParams(

                        request.getTicketNumOrPartyName(),
                        base.getOrganizationCode()
                );
        List<SimpleTicketDTO> dtos = new ArrayList<>();
        //参数转换
        models.forEach(target -> {
            SimpleTicketDTO dto = new SimpleTicketDTO();
            dto.setTicketId(target.getTicketId());
            dto.setTicketNum(target.getTicketNum());
            dto.setTicketStatus(target.getTicketStatus());
            dto.setPartyName(target.getPartyName());
            dto.setPartySiteName(target.getPartySiteName());
            dtos.add(dto);
        });
        response.setSimpleTicketDTOS(dtos);
        return response;
    }
}
